package com.base.cn.platform.os.service.statistics.goods;

import com.base.cn.platform.os.common.mybatis.BaseBiz;
import com.base.cn.platform.os.common.utils.DateUtils;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.dao.statistics.goods.ZStatisticsGoodsPVCountDao;
import com.base.cn.platform.os.entity.statistics.goods.ZStatisticsGoodsPVCount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 商品PV统计Biz
 *
 * @author s.li
 * @date 2019/04/28
 */
@Service
public class ZStatisticsGoodsPVCountBiz extends BaseBiz<ZStatisticsGoodsPVCount, ZStatisticsGoodsPVCountDao> {

    private static final String TIME_FORMAT = "yyyy-MM-dd";
    @Autowired
    private ZStatisticsGoodsPVCountDao zStatisticsGoodsPVCountDao;

    /**
     * 生成商品PV统计记录
     * @param goodsPVCountList
     */
    @Transactional(rollbackFor = IllegalArgumentException.class)
    public synchronized void genGoodsPVCountList(List<ZStatisticsGoodsPVCount> goodsPVCountList){
        Date nowDate = new Date();
        //获取列表中的时间条件
        String timeWhereSql = goodsPVCountList.stream().map(e-> "countTime ='"+ DateUtils.format(e.getCountTime(), TIME_FORMAT)+"'").collect(Collectors.joining(" or "," 1=1 and (",")"));
        //查询原有的数据
        List<ZStatisticsGoodsPVCount> pvCountList = this.find(timeWhereSql,null,null);
        List<ZStatisticsGoodsPVCount> updateList = new ArrayList<>();
        List<ZStatisticsGoodsPVCount> addList = new ArrayList<>();
        if(ObjectUtils.isNotEmpty(pvCountList)){//如果原有的数据存在
            Map<String,ZStatisticsGoodsPVCount> oldPVMap = pvCountList.stream().collect(Collectors.toMap(e->DateUtils.format(e.getCountTime(), TIME_FORMAT)+"_"+e.getGoodsType()+"_"+e.getGoodsId(), e->e));
            Map<String,ZStatisticsGoodsPVCount> newPVMap = goodsPVCountList.stream().collect(Collectors.toMap(e->DateUtils.format(e.getCountTime(), TIME_FORMAT)+"_"+e.getGoodsType()+"_"+e.getGoodsId(), e->e));
            newPVMap.forEach((k,v)->{
                ZStatisticsGoodsPVCount oldData = oldPVMap.get(k);
                if(ObjectUtils.isNotEmpty(oldData)){
                    oldData.setPvCount(v.getPvCount());
                    oldData.setPvInfo(v.getPvInfo());
                    oldData.setUpdateTime(nowDate);
                    updateList.add(oldData);
                }else{
                    addList.add(v);
                }
            });
        }else{
            addList.addAll(goodsPVCountList);
        }
        if(ObjectUtils.isNotEmpty(addList)){
            this.batchSave(addList);
        }
        if(ObjectUtils.isNotEmpty(updateList)){
            this.updateBatch(updateList);
        }
    }

    /**
     * 查询商品PV，以天为单位
     * @param timeSql
     * @return List<ZStatisticsGoodsPVCount>
     */
    public List<ZStatisticsGoodsPVCount> queryGoodsPVCountList(String timeSql){
        return zStatisticsGoodsPVCountDao.queryGoodsPVCountList(ZStatisticsGoodsPVCount.class,timeSql);
    }
}
